﻿using System;
using System.Collections.Generic;
using System.Data;
using BLL;
using Utils;
namespace UI.ModMini
{
    public partial class HostelAdd : System.Web.UI.Page
    {
        string hostelCode = "";
        string account = "";
        MiniBiz miniBiz = new MiniBiz();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (Request.Params["HostelCode"] != null)
            {
                hostelCode = Request.Params["HostelCode"].ToString();
                btnAdd.Enabled = false;
            }
            else
            {
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            if (!IsPostBack)
            {
                btnIn.Attributes.Add("onclick", "javascript:ShowLink('HostelEmpAdd.aspx',640,480);");
                JSUtility.AddConfirm(btnDelete, "您真的要删除该信息吗？");
                if (hostelCode != "")
                {
                    btnAdd.Enabled = false;
                    btnDelete.Enabled = true;
                    btnUpdate.Enabled = true;
                    BindData(hostelCode);
                }
            }
        }
        private void BindData(string hostelCode)
        {
            DataTable dt = miniBiz.GetOneHostel(hostelCode);
            if (dt.Rows.Count > 0)
            {
                txtHostelCode.Text = hostelCode;
                txtHostelCode.ReadOnly = true;
                new DefDicBiz().InitDropDownList(ddlHostelsex,dt.Rows[0]["Hostelsex"].ToString());
                txtAddr.Text = dt.Rows[0]["Addr"].ToString();
                txtReadyNum.Text = dt.Rows[0]["ReadyNum"].ToString();
                txtInNum.Text = dt.Rows[0]["InNum"].ToString();               
                txtMemo.Text = dt.Rows[0]["Memo"].ToString();

                //in member
                gvList.DataSource=miniBiz.GetEmpByHostel(hostelCode, "Y");
                gvList.DataBind();
            }

        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string code = txtHostelCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(code))
            {
                JSUtility.Alert(this, "编号必须填写");
                return;
            }
            if (miniBiz.IsExistHostelCode(code))
            {
                JSUtility.Alert(this, "编号已经存在");
                return;
            }
          string readyNum= txtReadyNum.Text.Trim();
          string inNum = txtInNum.Text.Trim();
          if (!RegexRule.IsInt(readyNum))
          {
              JSUtility.Alert(this, "额定人数请输入数字");
              return;
          }
          if (!RegexRule.IsInt(inNum))
          {
              JSUtility.Alert(this, "已经入住人数请输入数字");
              return;
          }
          SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
          sdList.Add("HostelCode", code);
          sdList.Add("ReadyNum", readyNum);
          sdList.Add("InNum", inNum);
          sdList.Add("Hostelsex", ddlHostelsex.SelectedValue);
          sdList.Add("Addr", txtAddr.Text.Trim());
          sdList.Add("Memo", txtMemo.Text.Trim());
          sdList.Add("Account", account);
          if(miniBiz.Insert("Mini_Hostel",sdList))
               JSUtility.Alert(this, "增加成功");
            else
              JSUtility.Alert(this, "增加失败");
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            Response.Redirect("HostelAdd.aspx");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string code = txtHostelCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(code))
            {
                JSUtility.Alert(this, "编号必须填写");
                return;
            }
            if (!miniBiz.IsExistHostelCode(code))
            {
                JSUtility.Alert(this, "编号不存在，不能更新");
                return;
            }
            string readyNum = txtReadyNum.Text.Trim();
            string inNum = txtInNum.Text.Trim();
            if (!RegexRule.IsInt(readyNum))
            {
                JSUtility.Alert(this, "额定人数请输入数字");
                return;
            }
            if (!RegexRule.IsInt(inNum))
            {
                JSUtility.Alert(this, "已经入住人数请输入数字");
                return;
            }
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("HostelCode", code);
            sdList.Add("ReadyNum", readyNum);
            sdList.Add("InNum", inNum);
            sdList.Add("Hostelsex", ddlHostelsex.SelectedValue);
            sdList.Add("Addr", txtAddr.Text.Trim());
            sdList.Add("Statu", "Y");
            sdList.Add("Memo", txtMemo.Text.Trim());
            sdList.Add("Account", account);
            if (miniBiz.Update("Mini_Hostel", sdList))
                JSUtility.Alert(this, "更新成功");
            else
                JSUtility.Alert(this, "更新失败");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(hostelCode))
            {
                if (miniBiz.DeleteHostel(hostelCode))
                    JSUtility.Alert(this, "删除成功");
                else
                    JSUtility.Alert(this, "删除失败");
            }
            else
                JSUtility.Alert(this, "没有选择信息");
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            JSUtility.CloseWindow();
        }

        protected void btnNoUse_Click(object sender, EventArgs e)
        {
            string code = txtHostelCode.Text.Trim();
            if (string.IsNullOrWhiteSpace(code))
            {
                JSUtility.Alert(this, "编号必须填写");
                return;
            }
            if (!miniBiz.IsExistHostelCode(code))
            {
                JSUtility.Alert(this, "编号不存在，不能更新");
                return;
            }
          
            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("HostelCode", code);          
            sdList.Add("Statu", "N");
            sdList.Add("StatuTime", System.DateTime.Now.ToString("yyyy-MM-dd"));
            sdList.Add("Account", account);
            if (miniBiz.Update("Mini_Hostel", sdList))
                JSUtility.Alert(this, "更新成功");
            else
                JSUtility.Alert(this, "更新失败");
        }
    }
}